<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>state的简写方式</title>
</head>

<body>
    <!--准备好一个容器-->
    <div id="test"></div>
    <!--引入核心库-->
    <script src="../js/react.development.js"></script>
    <!--引入react-dom支持react操纵dom-->
    <script src="../js/react-dom.development.js"></script>
    <!--引入babel用于将jsx转为js-->
    <script src="../js/babel.min.js"></script>

    <script type="text/babel">
        class Weather extends React.Component {
            //初始化状态
            state = { isHot: true }//直接在类中添加属性
            render() {
                const { isHot } = this.state
                return <h1 onClick={this.changeWeather}>今天天气{isHot ? "很热" : "很冷"}</h1>
            }
            //自定义方法要用赋值语句的形式加箭头函数
            changeWeather = () => {//箭头函数没有自己的this 会绑定函数体外侧的this
                const isHot = this.state.isHot;
                this.setState({ isHot: !isHot });//这里必须使用setState方法重新给state赋值
            }
        }
        //2.渲染到页面
        ReactDOM.render(<Weather />, document.getElementById('test'));
    </script>
</body>

</html>